﻿using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class LoginService
    {
        //用户登录
        public static Users UserLogin(string login, string pwd)
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                return db.Users.FirstOrDefault(u => u.U_Email == login && u.U_Pwd == pwd);
            }
        }
        //登陆页面的大家都在说
        public static List<MicroBlog> GetMicroBlogs()
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                return db.MicroBlog.ToList();
            }
        }

        //获取UserInfo，传值到用户信息页面
        public static UserInfo GetUserInfo(string login)
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                int UserID = db.Users.FirstOrDefault(u => u.U_Email == login).U_ID;
                return db.UserInfo.FirstOrDefault(u => u.U_ID == UserID);
            }
        }

        //获取Users，传值到用户信息页面
        public static Users GetUsers(string login)
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                int UserID = db.Users.FirstOrDefault(u => u.U_Email == login).U_ID;
                return db.Users.FirstOrDefault(u => u.U_ID == UserID);
            }
        }

        //保存用户信息
        public static bool SaveUserInfo(UserInfo user)
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                try
                {
                    db.UserInfo.Add(user);
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    return false;
                    throw;
                }
            }
            return true;
        }
    }
}
